package com.protocolmeter;

import com.protocolmeter.STS.Get_Token;
import com.protocolmeter.frame.Meter;
import com.protocolmeter.frame.Meter645;
import com.protocolmeter.frame.Meterabroad;
import com.protocolmeter.frame.Meterwater;
import com.protocolterminal.function.Check3761;
import com.protocolterminal.function.CheckFrame;

/**
 * @ClassName: ProtocolServer
 * @Description: 协议的服务类，所有对外接口均在这里体现
 * @author baozhijian
 * @date 2017年9月8日 下午1:50:08
 * @version V1.0
 */
public class ProtocolMerterService {

	private CheckFrame checkFrame;

	public boolean Check(String frame) {
		this.checkFrame = new Check3761();
		return checkFrame.Check(frame);
	}

	// 集中器协议，表协议,相关参数
	public String Encode(int meterprotocol, String meteraddress, String control, String identification, String ope,
			String data, String psw, String keys) {
		// 组数据域
		Meter meter = null;
		switch (meterprotocol) {
		case 1:// 645
			meter = new Meter645(meteraddress, control, identification, psw, ope, data);
			break;
		case 2:// 水表
			meter = new Meterwater(meteraddress, 10);
			break;
		case 3:// 海外表
			meter = new Meterabroad(meteraddress, identification, data);
			break;
		case 4:// 迁安表
			meter = new Meter645(meteraddress, control, identification, psw, ope, data);
			break;
		case 5:// STS表
				// STS加密，获取token值
			if (identification.equalsIgnoreCase("04000108") || identification.equalsIgnoreCase("04000109")
					|| identification.equalsIgnoreCase("04000402") || identification.equalsIgnoreCase("04001002")
					|| identification.equalsIgnoreCase("040501FF") || identification.equalsIgnoreCase("040502FF")
					|| identification.equalsIgnoreCase("04060500") || identification.equalsIgnoreCase("04060501")
					|| identification.equalsIgnoreCase("04060502") || identification.equalsIgnoreCase("04001006")
					|| control.equalsIgnoreCase("1C") || identification.equalsIgnoreCase("070301FF")
					|| identification.equalsIgnoreCase("070103FF") || identification.equalsIgnoreCase("070101FF")
					|| identification.equalsIgnoreCase("070102FF")) {

				Get_Token get_Token = new Get_Token();
				data = get_Token.GetToken(control, identification, data, keys);
				// 组帧
				if (control.equalsIgnoreCase("00")) {
					return data.toString();
				}
				if (identification.equalsIgnoreCase("04001006") || control.equalsIgnoreCase("1C")) {
					psw = "00000098";
				}
				if (identification.equalsIgnoreCase("04000108") || identification.equalsIgnoreCase("04000109")
						|| identification.equalsIgnoreCase("04000402") || identification.equalsIgnoreCase("04001002")
						|| identification.equalsIgnoreCase("040501FF") || identification.equalsIgnoreCase("040502FF")
						|| identification.equalsIgnoreCase("04060500") || identification.equalsIgnoreCase("04060501")
						|| identification.equalsIgnoreCase("04060502")) {

					psw = "00000099";
				}
			}

			meter = new Meter645(meteraddress, control, identification, psw, ope, data);

			break;
		default:
			meter = new Meter645(meteraddress, control, identification, psw, ope, data);
			break;
		}

		return meter.toString();
	}

	public String Decode(int meterprotocol, String sentdata, String returndata) {

		Meter meter = null;
		switch (meterprotocol) {
		case 1:// 普通电表
			meter = new Meter645(sentdata, returndata);
			break;
		case 2:// 水表
			meter = new Meterwater(sentdata, returndata);
			break;
		case 3:// 海外表
			meter = new Meterabroad(sentdata, returndata);
			break;
		case 4:// 迁安表
			meter = null;
			break;
		case 5:// STS表
			meter = new Meter645(sentdata, returndata);
			break;
		default:
			meter = new Meter645(sentdata, returndata);
			break;
		}

		return meter.Getdata();
	}

}
